//
//  foundQuestDXTableViewCell.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/8/1.
//

#import "foundQuestDXTableViewCell.h"
#import "foundQuestModel.h"
#import "YTBaseSelectStringView.h"
#import "YTBaseEditorStringView.h"
#import "YTSelectStringShowView.h"
#import "DigitalInputView.h"

@interface foundQuestDXTableViewCell ()
@property (nonatomic ,strong) UIView * backview;
@property (nonatomic ,strong) UIView * nameview;
@property (nonatomic ,strong) UIView * makeview;
@property (nonatomic ,strong) UIView * phoneview;

@property (nonatomic ,strong) UILabel * namelab;

//收缩的按钮
@property (nonatomic ,strong) UIButton * sxBtn;

@end
@implementation foundQuestDXTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self setupView];
    }
    return self;
}


- (void)setupView{
    
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.whiteColor;
    self.userInteractionEnabled = YES;
    self.contentView.userInteractionEnabled = YES;

    UIView * backview = [UIView new];
    self.backview = backview;
    [self.contentView addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.right.offset(kwt(-10));
        make.top.offset(0);
        make.bottom.offset(kwt(-5));
    }];
    UIView * phoneview = [UIView new];
    [self.backview addSubview:phoneview];
    self.phoneview = phoneview;
    phoneview.backgroundColor = UIColor.whiteColor;

    
    UIView * nameview = [UIView new];
    self.nameview = nameview;
    [backview addSubview:nameview];
    [nameview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.offset(kwt(35));
    }];
    
    UIButton * sxBtn = [UIButton buttonWithType:0];
    self.sxBtn = sxBtn;
    sxBtn.addTarget(self,@selector(openSXBtnClick));
    [sxBtn setImage:[UIImage imageNamed:@"template_expand"] forState:UIControlStateNormal];
    [sxBtn setImage:[UIImage imageNamed:@"template_retract"] forState:UIControlStateSelected];
    [nameview addSubview:sxBtn];
    [sxBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.width.height.offset(kwt(20));
        make.right.offset(kwt(-10));
    }];
    
    UILabel * namelab = [DTKTool labelWithText:@"-" andFont:kFont(17) andTextColor:KcolorHex(@"#222222")];
    self.namelab = namelab;
    [nameview addSubview:namelab];
    [namelab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.left.offset(kwt(10));
    }];
    
    UIView * makeview = [UIView new];
    makeview.clipsToBounds = YES;
    self.makeview = makeview;
    [backview addSubview:makeview];
    [makeview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.equalTo(nameview.mas_bottom);
    }];
    
}

- (void)openSXBtnClick{
    self.model.isSQ = !self.model.isSQ;
    if(self.ReloadBlock){
        self.ReloadBlock();
    }
}

- (void)setModel:(pondModelListModel *)model{
    _model = model;
    self.namelab.text = model.name;
    [self.makeview.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    self.sxBtn.selected = model.isSQ;
    ///大一点
    self.phoneview.frame = CGRectMake(-1, 0, kwt(355) + 2, model.RWcellH - kwt(5) + 1);
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    // 使用UIBezierPath创建圆角矩形路径
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.phoneview.bounds cornerRadius:kwt(5)];
    // 设置形状层的路径
    shapeLayer.path = path.CGPath;
    // 设置虚线样式
    shapeLayer.strokeColor = KcolorHex(@"#BEBEBE").CGColor; // 边框颜色
    shapeLayer.lineWidth = 0.5; // 边框宽度
    shapeLayer.lineDashPattern = @[@3, @3]; // 定义虚线模式，这里是4个单位的线，接着4个单位的空
    // 设置填充颜色为空，因为我们只需要边框
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    self.phoneview.layer.sublayers = nil;
    [self.phoneview.layer addSublayer:shapeLayer];

    
    MJWeakSelf
    CGFloat Y = 0;
    for (TemplateFoundDetailsTaskItemModelListModel * taskmodel in model.taskItemModelList) {
        /// 样式;1:多项选择 2:下单单选3:数字输入框4:单行文本 5:多行文本
        if(taskmodel.pattern == 1){
            UILabel * leftLab = [DTKTool labelWithText:@"" andFont:kCFont(17) andTextColor:KcolorHex(@"#222222")];
            leftLab.attributedText = [DTKTool AttributedStringWithStr:[NSString stringWithFormat: @"%@%@",((taskmodel.forceFlag == 1)?@"*":@""),taskmodel.name] andNodeColor:KcolorHex(@"#222222") andSeledColor:KcolorHex(@"#FA0F0F") andFont:kCFont(17) andPattenr:@"[*]"];
            [self.makeview addSubview:leftLab];
            [leftLab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.offset(kwt(10));
                make.top.offset(kwt(14) + Y);
            }];
            UILabel * rightLab = [DTKTool labelWithText:@" (可多选)" andFont:kCFont(17) andTextColor:KcolorHex(@"#222222")];
            [self.makeview addSubview:rightLab];
            [rightLab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(leftLab.mas_right);
                make.centerY.equalTo(leftLab);
            }];
            
            CGFloat X = 0;
            CGFloat tempY = kwt(45) + Y;
            for (TemplateFoundDetailsItemModelListModel * childModel in taskmodel.itemModelList) {//子选项
                UIView * btnview = [UIView new];
                btnview.addClickBlock(^{
                    childModel.checkedFlag = (childModel.checkedFlag == 1?0:1);
                    if(weakSelf.ReloadBlock){
                        weakSelf.ReloadBlock();
                    }
                });
                [DTKTool YJBK:kwt(6) andView:btnview andWidth:1 andColor:childModel.checkedFlag?KcolorHex(@"#0967FF"):KcolorHex(@"#BFBFBF")];
                UILabel * lab = [DTKTool labelWithText:childModel.name andFont:kFont(16) andTextColor:childModel.checkedFlag?KcolorHex(@"#0967FF"):KcolorHex(@"#373C45")];
                [btnview addSubview:lab];
                lab.textAlignment = NSTextAlignmentCenter;
                [lab mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.offset(kwt(10));
                    make.right.offset(kwt(-10));
                    make.centerY.offset(0);
                }];
                if(childModel.checkedFlag){
                    UIImageView * xzimg = kimageView(@"template_selete");
                    [btnview addSubview:xzimg];
                    [xzimg mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.right.bottom.offset(0);
                        make.height.offset(kwt(11));
                        make.width.offset(kwt(10));
                    }];
                }
                [self.makeview addSubview:btnview];
                if(X + childModel.nameW > kwt(330)){
                    X = 0;
                    tempY += kwt(48);
                }
                btnview.frame = CGRectMake(X + kwt(10), tempY, childModel.nameW, kwt(32));
                X += childModel.nameW + kwt(14);
            }
            
        } 
        else if(taskmodel.pattern == 2){///下单选
            YTBaseSelectStringView * view = [YTBaseSelectStringView NewView];
            view.name = taskmodel.name;
            view.forceFlag = taskmodel.forceFlag;
            view.content = taskmodel.itemSeledModel.name;
            view.makeColor = UIColor.whiteColor;
            view.block = ^(NSString * _Nonnull content) {
                NSMutableArray * array = [NSMutableArray array];
                for (TemplateFoundDetailsItemModelListModel * itemModel in taskmodel.itemModelList) {
                    [array addObject:itemModel.name];
                }
                [YTSelectStringShowView showViewWithTitle:[NSString stringWithFormat:@"选择%@",taskmodel.name] andIndex:0 andDateArray:array andBlock:^(NSString * _Nonnull str) {
                    for (TemplateFoundDetailsItemModelListModel * itemModel in taskmodel.itemModelList) {
                        if([itemModel.name isEqualToString:str]){
                            itemModel.checkedFlag = 1;
                        } else{
                            itemModel.checkedFlag = 0;
                        }
                    }
                    if(weakSelf.ReloadBlock){
                        weakSelf.ReloadBlock();
                    }
                }];               
            };
            [self.makeview addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.offset(0);
                make.top.offset(Y);
                make.height.offset(kwt(45));
            }];
        }
        else if(taskmodel.pattern == 3){
            UIView * view = [UIView new];
            [self.makeview addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.offset(0);
                make.top.offset(Y);
                make.height.offset(kwt(45));
            }];
            UILabel * leftLab = [DTKTool labelWithText:@"" andFont:kCFont(17) andTextColor:KcolorHex(@"#222222")];
            leftLab.attributedText = [DTKTool AttributedStringWithStr:[NSString stringWithFormat: @"%@%@",((taskmodel.forceFlag == 1)?@"*":@""),taskmodel.name] andNodeColor:KcolorHex(@"#222222") andSeledColor:KcolorHex(@"#FA0F0F") andFont:kCFont(17) andPattenr:@"[*]"];
            [view addSubview:leftLab];
            [leftLab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.offset(kwt(10));
                make.centerY.offset(0);
            }];
            
            DigitalInputView * Inputview = [DigitalInputView NewView];
            [view addSubview:Inputview];
            Inputview.model = taskmodel;
            Inputview.openHUDBlock = weakSelf.openHUDBlock;
            Inputview.openReloadBlock = ^{
                if(weakSelf.ReloadBlock){
                    weakSelf.ReloadBlock();
                }
            };
            [Inputview mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.offset(kwt(-10));
                make.centerY.offset(0);
                make.width.offset(kwt(125));
                make.height.offset(kwt(25));
            }];
            
            
        } else if(taskmodel.pattern == 4 || taskmodel.pattern == 5){
            YTBaseEditorStringView * view = [YTBaseEditorStringView NewView];
            view.name = taskmodel.name;
            view.forceFlag = taskmodel.forceFlag;
            view.content = taskmodel.content;
            view.makeColor = UIColor.whiteColor;
            view.block = ^(NSString * _Nonnull content) {
                taskmodel.content = content;
                if(weakSelf.ReloadBlock){
                    weakSelf.ReloadBlock();
                }
            };
            [self.makeview addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.offset(0);
                make.top.offset(Y);
                make.height.offset(kwt(45));
            }];
        }
        Y += taskmodel.RWDXXZViewH;
        if(taskmodel != model.taskItemModelList.lastObject){
            UIView * line = [UIView new];
            line.backgroundColor = KcolorHex(@"#E5E3E3");
            [self.makeview addSubview:line];
            [line mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.offset(kwt(10));
                make.right.offset(kwt(10));
                make.top.offset(Y-0.5);
                make.height.offset(0.5);
            }];
        }
        
    }
    
}
@end
